<?php
namespace app\controller;
use tools\Model;

class ArticleController extends CommonController {
    public function index(){
        $data=(new Model())->query('select * from article');

        include './view/index.html';
    }



    /**
     * 增加文章功能
     *
     */
    public function add(){
        if(IS_POST){
            $time=time();
            $sql="insert into article (title,content,time) values ('{$_POST['title']}','{$_POST['content']}','{$time}')";
            (new Model())->exec($sql);
//            上传显示
            $this->redirect('?a=index','上传成功');
        }
        include './view/addShow.html';
    }

    /**
     * 编辑页面功能
     *
     */
    public function edit(){
//        抓取当前ID
        $id=$_GET['id'];
//        存入旧数据
        $oldData=(new Model())->query_edit("select * from article where id={$id}");
        print_r($oldData);
        if(IS_POST){
//            重复下面操作,将数据存入数据库
            $time=time();
            $sql="replace into article (id,title,content,time) values ('{$id}','{$_POST['title']}','{$_POST['content']}','{$time}')";
            (new Model())->exec($sql);
            $this->redirect('?a=index','编辑成功');
        }
        include './view/editShow.html';
    }

    /**
     * 删除页面功能
     *
     */
    public function del(){
        $id=$_GET['id'];
        $sql="delete from article where id={$id}";
        (new Model())->exec($sql);
        $this->redirect ('?a=index','删除成功');


    }

    /**
     * 显示页面功能
     */
    public function show(){
        $id=$_GET['id'];
        $nowData=$this->data[$id];
        include './view/show.html';

    }
}